<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


<%@page language="java"
	contentType="text/html" 
	pageEncoding="UTF-8"
	import="com.example.validate.UserBean"	
%>

<% 
	String matcher = request.getParameter("id");
	if(!matcher.matches("^[0-9]*$")){
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}
%>

<sql:query var="reviews" dataSource="jdbc/lut2">
    SELECT * FROM user_reviews, school
    WHERE user_reviews.school_id = school.school_id
    AND school.school_id = ? <sql:param value = "${param.id}"/>
</sql:query>

<sql:query var="school" dataSource="jdbc/lut2">
    SELECT full_name, short_name FROM school
    WHERE school_id = ? <sql:param value = "${param.id}"/>
</sql:query>


<!-- Aurora lagt til. Test -->
<% 
	UserBean currentUser = (UserBean) session.getAttribute("currentSessionUser");
	String userName = "Not logged in";
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="lutstyle.css">
        <c:set var="school" value ="${school.rows[0]}"/>
        <title>Reviews for ${school.full_name} </title>
        
    </head>
    <body>
        <h1>Reviews for ${school.short_name}</h1>

        <!-- looping through all available reviews - if there are any -->
        <c:set var="review" value="${reviews.rows[0]}"/>
        
        <c:choose>
            <c:when test="${ empty review }">
                No reviews for ${school.full_name} yet. Help us out by adding one!
              
                <br><br>
            </c:when>
            <c:otherwise>
                <c:forEach var="review" items="${reviews.rowsByIndex}">
                   	${review[2]}<br>
                    <i>${review[1]}</i>
                    
                     <!--<c:out value="${review[2]}"/><br> 
                    <i><c:out value="${review[2]}"/></i>-->
                    <br><br>
                </c:forEach>
            </c:otherwise>
        </c:choose>
        
        <%
        if(currentUser == null){
        //user is not logged in, should have a better check than just check if null
    		out.print("<form action=\"loginPage.jsp\">You need to <button type=\"submit\">log in</button> to add a review</form>");
	    }else{
	    //user is logged in
	    userName = currentUser.getUsername();
	    }

        %>
  
        <table border="0">
            <thead>
                <tr>
                    <th colspan="2">Help improving LUT2.0 by adding a review of ${school.short_name}</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <form action="AddReviewServlet"  method="post">
                            <input type="hidden" name="school_id" value="${param.id}" />
                            <textarea name="review" rows=10 cols=60 wrap="physical" autofocus="on" > 
                            </textarea>
                            <br><br>
                             <%     
                             	if(currentUser == null){

    							}else{ //user is logged in
							%>
							<!-- Her må vi endre fra hidden når sessions fungerer -->
    								<input type="hidden" name="name" value= "<% out.print(userName); %>"/>
                                <br><br>
                                <input type="submit" value="Add review" />    	
                            <% } %>
                            
                        </form>
                    </td>
                </tr>
            </tbody>
        </table>

    </body>
</html>
